home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-01-06 | 5.3 KB | 153 lines | [TEXT/TPAS] |
- { A demonstration of the Serial Drivers. Written for Turbo Pascal, but }
- { should be easily transported to other Pascals. 01/06/88 }
- { Written by Michael South, Nautilus Data Services, (509) 545-5424. }
-
- PROGRAM SerDemo;
- {$T APPL????} { Set program file type and creator }
-
- USES Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf;
-
- CONST
- AscCR = 13; { ASCII value of a carriage return }
- AscXON = 17; { ASCII for DC1 (Control-Q) }
- AscXOFF = 19; { ASCII for DC3 (Control-S) }
-
- VAR modemIn : INTEGER; { Driver reference # for modem port }
- theEvent : EventRecord;
- dummyB : BOOLEAN;
-
- { Open the modem port for input }
-
- PROCEDURE Init;
- VAR handshake : SerShk;
- BEGIN
- IF RAMSDOpen(sPortA)<>noErr { For compatibility w/ older Macs }
- THEN WriteLn('Cannot open RAM driver');
- IF OpenDriver('.AIn',modemIn)<>noErr
- THEN WriteLn('Cannot open modem port');
- IF SerReset(modemIn, baud1200+stop10+noParity+data8)<>noErr
- THEN WriteLn('Cannot set baud/data format');
- WITH handshake DO
- BEGIN
- fXon := 0; { No output flow control }
- xOn := Char(AscXOn); { Xon character }
- xOff := Char(AscXOff); { Xoff character }
- errs := 0; { Ignore framing/overrun errors }
- evts := 0; { No device driver events created }
- fInX := 1 { Enable Xon/Xoff input flow control }
- END;
- IF SerHShake(modemIn, handshake)<>noErr
- THEN WriteLn('Cannot set handshaking')
- END;
-
- { ========== First method of reading a line from the serial port ========== }
-
- PROCEDURE ReadStr1;
- VAR MyString : STR255;
- DoneWithLine: BOOLEAN; { When true, a complete line has been recieved }
- AChar : BOOLEAN; { Actually, a 1-byte char }
- NumRead : LONGINT; { How many characters to read/were read }
- BEGIN
- MyString:='';
- DoneWithLine := FALSE;
-
- { Keep getting characters until CR, or 255 characters have been read, }
- { or the mouse button is pushed. }
-
- WHILE (NOT DoneWithLine) AND (NOT Button) DO
- BEGIN
- IF SerGetBuf(modemIn, NumRead)<>noErr
- THEN WriteLn('Cannot get # of chars in buffer');
- IF NumRead>0
- THEN
- BEGIN
- NumRead := 1; { Only get 1 char }
- IF FSRead(modemIn, NumRead, @AChar)<>noErr
- THEN WriteLn('Cannot read modem port');
- IF NumRead>0
- THEN
- BEGIN
- MyString :=
- Concat(MyString, Char(Ord(AChar)));
- IF (Ord(AChar)=AscCR) OR
- (Length(MyString)=255)
- THEN DoneWithLine:=TRUE
- END
- END;
- END;
-
- IF DoneWithLine THEN WriteLn(MyString)
-
- END;
-
- { ========= Second method of reading a line from the serial port ========== }
-
- PROCEDURE ReadStr2;
-
- VAR MyString : STR255;
- NumRead : LONGINT; { How many characters to read/were read }
- TempBPtr : ^BOOLEAN; { Used when forcing length of MyString }
-
- { The following field is used to move an integer into an unsigned byte,}
- { without Pascal doing unwanted sign extension. }
- Int2UByte : RECORD
- CASE INTEGER OF
- 0 : (int : INTEGER);
- 1 : (dumm : BOOLEAN;
- ubyt : BOOLEAN)
- END;
-
- { NOTE: The type BYTE (an unsigned byte) is actually stored as a *word* }
- { (unless it's part of a packed structure). BOOLEAN is the only }
- { unpacked data type that is stored as a single byte. }
-
- BEGIN
-
- { Freeze computer until 255 characters have been read }
-
- NumRead := 255; { Get 255 char }
- IF FSRead(modemIn, NumRead, POINTER(ORD4(@MyString)+1))<>noErr
- THEN WriteLn('Cannot read modem port');
-
- { Force the length of MyString }
-
- Int2UByte.int := NumRead;
- TempBPtr := POINTER(ORD4(@MyString)); { Points to length byte }
- TempBPtr^ := Int2UByte.ubyt; { Have Pascal move a single byte }
-
- WriteLn(Button, MyString)
-
- END;
-
- PROCEDURE WrapUp;
- BEGIN
- IF CloseDriver(modemIn)<>noErr THEN WriteLn('Cannot close');
- RAMSDClose(sPortA)
- END;
-
- BEGIN
- Init;
- REPEAT
- BEGIN
- ReadStr1;
- {ReadStr2;}
- dummyB:=GetNextEvent(everyEvent,theEvent)
- END;
- UNTIL theEvent.what=mouseDown;
- WrapUp
- END.
-
- { Caveats:
- This program works OK at 1200 baud. However, as the baud rate increases the
- Mac spends more and more time drawing the characters on the screen. The
- limit, using Pascal and simple WriteLns, is about 1200 baud.
-
- At speeds over 1200 baud, it is important that the sending computer and the
- Mac use some type of "flow control". The most common is Xon-Xoff. It allows
- one party to tell the other to "wait a minute" (x-off) and "go ahead" (x-on).
- The above routine is configured for X-on/x-off.
-
- If flow control is not used or working, then a lot of garbage will be
- recieved.
- }
-